import requests
from bs4 import BeautifulSoup
from news_2.src.main.utils.file_tool import read_file_contents

"""
html网页解析类
"""


def extract_news_bat(json_data):
    for key, value in json_data.items():
        print("-" * 50)
        new_extractor = NewsExtractor(key, value)
        if new_extractor.is_open == 1:
            print(f"正在抓取{key} ")
            result = new_extractor.extract_news(key, value)
            new_extractor.send_msg(result)


class NewsExtractor:
    def __init__(self, encoding="utf-8", key="", data=None):
        if data is None:
            data = {}
        self.encoding = encoding
        self.key = key
        self.is_open = data['is_open'] | 0
        self.url = data['url'] | ""
        self.title_path = data['title_dict'] | ""
        self.title_value = data['title_value'] | ""
        self.title_link = data['title_link'] | ""
        self.time_path = data['time_dict'] | ""
        self.time_value = data['time_value'] | ""

    def __get_html_text(self, url):
        try:
            response = requests.get(url)
            if 200 <= response.status_code < 300:
                return response.text
            else:
                print('请求失败:', response.status_code)
                return None
        except requests.exceptions.RequestException as e:
            print('请求异常:', e)

    def __get_html_tag(self, element, root):
        if 'id' in root:
            element = element.find(id=root['id'])
        elif 'attrs' in root and 'tag_name' in root:
            element = element.find(root['tag_name'], attrs=root['attrs'])
        elif 'tag_name' in root:
            element = element.find(root['tag_name'])
        if element and 'children' in root:
            return self.__get_html_tag(element, root['children'])
        return element

    def __get_data_from_label(self, label, attribute=None, value=None):
        if label is None:
            return None
        if attribute:
            return label.get(attribute, None)
        if value == 1:
            return label.text
        return None

    def __get_data_from_label_path(self, label, path):
        attribute = path['attribute']
        value = path['value']
        return self.__get_data_from_label(label, attribute, value)

    def extract_news(self, key, value):
        text = read_file_contents(f"../resources/{key}.txt")
        if text is None or text == "":
            return None
        if "encode" in value and value['encode']:
            try:
                text = text.encode(value['encode']).decode(self.encoding)
            except Exception as e:
                print(f"解码异常 '{value['encode']}': {e}")
        soup = BeautifulSoup(text, 'html.parser')
        title_tag = self.__get_html_tag(soup, value['title_dict'])
        title_value = self.__get_data_from_label_path(title_tag, value['title_value'])
        title_link = self.__get_data_from_label_path(title_tag, value['title_link'])
        time_tag = self.__get_html_tag(soup, value['time_dict'])
        time_value = self.__get_data_from_label_path(time_tag, value['time_value'])
        result = (f"标题: {title_value}\n"
                  f"地址: {title_link}\n"
                  f"发布时间: {time_value}\n")
        return result

    def send_msg(self, msg):
        if msg:
            print(f"机器人发送消息：\n{msg}")
